package com.example.reward.fragments;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.view.WindowInsetsControllerCompat;

import com.example.reward.R;
import com.example.reward.response.RewardResultDto;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

public class HistoryDetailActivity extends AppCompatActivity {
    private LinearLayout scoresContainer;
    private TextView currentRankView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_detail);

        // ✅ 关闭系统默认 Insets 处理
        WindowCompat.setDecorFitsSystemWindows(getWindow(), false);

        // ✅ 透明状态栏
        getWindow().setStatusBarColor(Color.TRANSPARENT);

        // ✅ 如果背景是浅色，状态栏字体变暗
        WindowInsetsControllerCompat controller = new WindowInsetsControllerCompat(getWindow(), getWindow().getDecorView());
        controller.setAppearanceLightStatusBars(true);

        // ✅ 动态处理 ScrollView padding
        View rootView = findViewById(R.id.historyDetailRoot);
        ViewCompat.setOnApplyWindowInsetsListener(rootView, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(v.getPaddingLeft(), systemBars.top, v.getPaddingRight(), systemBars.bottom);
            return insets;
        });

        // ✅ 初始化 UI
        TextView titleView = findViewById(R.id.detailTitle);
        TextView timeView = findViewById(R.id.detailTime);
        TextView contentView = findViewById(R.id.detailContent);
        TextView baseDetailView = findViewById(R.id.baseDetailContent);
        TextView extraDetailView = findViewById(R.id.extraDetailContent);
        TextView specialDetailView = findViewById(R.id.specialDetailContent);
        scoresContainer = findViewById(R.id.scoresContainer);
        currentRankView = findViewById(R.id.currentRankView);

        // ✅ 获取传递的数据
        RewardResultDto reward = getIntent().getParcelableExtra("rewardItem");
        if (reward == null) return;

        // ✅ 填充内容
        String title = "总奖励：" + reward.getTotalReward();
        String fullContent = "奖励总金额：" + reward.getTotalReward() +
                "\n总分：" + reward.getTotal() +
                "（核心：" + reward.getCoreTotal() +
                "，综合：" + reward.getGeneralTotal() + "）" +
                "\n当前排名：" + reward.getCurrentRank();
        String baseDetail = "基础奖励（" + reward.getBase() + "）\n达标条件：" + reward.getBaseDesc();
        String extraDetail = "额外奖励（" + reward.getExtra() + "）\n达标条件：" + reward.getExtraDesc();
        String specialDetail = "特殊奖励（" + reward.getSpecial() + "）\n达标条件：" + reward.getSpecialDesc();

        titleView.setText(title);
        timeView.setText(formatTimestamp(reward.getTimestamp()));
        contentView.setText(fullContent);
        baseDetailView.setText(baseDetail);
        extraDetailView.setText(extraDetail);
        specialDetailView.setText(specialDetail);

        // ✅ 动态展示科目成绩
        if (reward.getStudentDto() != null && reward.getStudentDto().getScores() != null) {
            scoresContainer.removeAllViews();
            for (Map.Entry<String, Integer> entry : reward.getStudentDto().getScores().entrySet()) {
                String subject = entry.getKey();
                Integer score = entry.getValue();
                TextView tv = new TextView(this);
                tv.setText(subject + " : " + score);
                tv.setTextSize(16f);
                tv.setPadding(0, 4, 0, 4);
                scoresContainer.addView(tv);
            }
        }

        // 当前排名显示
        currentRankView.setText("当前排名: " + reward.getCurrentRank());
    }

    private String formatTimestamp(long timestamp) {
        Date date = new Date(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        return sdf.format(date);
    }
}
